@page "/signup"
@inherits AppAuthComponentBase
@inject ServiceStackStateProvider AuthStateProvider
@inject NavigationManager NavigationManager

@if (IsAuthenticated)
{
    NavigationManager.NavigateTo(NavigationManager.GetReturnUrl(), true);
    return;
}

<div class="mb-4">
    <h1 class="text-2xl font-semibold text-gray-900 dark:text-gray-100">Sign Up</h1>
</div>

<div class="max-w-screen-md">

<form @onsubmit="submit" class="max-w-xl">
<CascadingValue Value=@api.Error>
    <div class="shadow overflow-hidden sm:rounded-md">
        <ErrorSummary Except=@VisibleFields />
        <div class="px-4 py-5 bg-white dark:bg-black space-y-6 sm:p-6">
          <div class="flex flex-col gap-y-4">
            <TextInput @bind-Value="request.DisplayName" spellcheck="false" Help="Your first and last name" required />
            <TextInput type="email" @bind-Value="request.Email" spellcheck="false" required />
            <TextInput type="password" @bind-Value="request.Password" Help="Password, 6 characters or more" required />
            <TextInput type="password" @bind-Value="request.ConfirmPassword" />
            <CheckboxInput @bind-Value="request.AutoLogin" />
          </div>
        </div>
        <div class="pt-5 px-4 py-3 bg-gray-50 dark:bg-gray-900 text-right sm:px-6">
          <div class="flex justify-end">
            <FormLoading class="flex-1" Loading=@api.IsLoading />

            <PrimaryButton type="submit">
                Sign Up
            </PrimaryButton>
          </div>
        </div>
    </div>
</CascadingValue>
</form>

<div class="flex mt-8 ml-8">
    <h3 class="mr-4 leading-8 text-gray-500">Quick Links</h3>
    <span class="relative z-0 inline-flex shadow-sm rounded-md">
        <SecondaryButton @onclick="SetUser">
            new@user.com
        </SecondaryButton>
    </span>
</div>

<div class="mt-4">
    <SrcLink href="https://github.com/NetCoreTemplates/blazor-tailwind/blob/master/MyApp.Client/Pages/SignUp.razor" IconSrc="/img/blazor.svg" />
</div>

</div>

@code {
    string[] VisibleFields => new[]{ 
        nameof(request.DisplayName), nameof(request.Email), nameof(request.Password), nameof(request.ConfirmPassword) };

    Register request = new();

    ApiResult<RegisterResponse> api = new();

    void SetUser()
    {
        request.DisplayName = "New User";
        request.Email = "new@user.com";
        request.Password = request.ConfirmPassword = "p@55wOrd";
        request.AutoLogin = true;
    }


    async Task submit()
    {
        api.ClearErrors();

        if (request.Email.IsNullOrEmpty())
            api.AddFieldError(nameof(Register.Email), "Email is required");

        if (request.Password.IsNullOrEmpty())
            api.AddFieldError(nameof(Register.Password), "Password is required");
        else if (request.ConfirmPassword != request.Password)
            api.AddFieldError(nameof(Register.ConfirmPassword), "Passwords do not match");

        if (api.Failed) return;

        api.IsLoading = true;
        api = await AuthStateProvider.RegisterAsync(request);

        if (api.Succeeded)
        {
            if (request.AutoLogin == true)
            {
                NavigationManager.NavigateTo(NavigationManager.GetReturnUrl(), forceLoad: true);
            }
            else
            {
                NavigationManager.NavigateTo("/signin?return=" + NavigationManager.GetReturnUrl(), forceLoad: true);
            }
        }
    }
} 